

package com.example.zy.tinkertest.service;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.example.zy.tinkertest.Utils.Utils;
import com.tencent.tinker.lib.service.DefaultTinkerResultService;
import com.tencent.tinker.lib.service.PatchResult;
import com.tencent.tinker.lib.util.TinkerLog;

import java.io.File;

/**
 * optional, you can just use DefaultTinkerResultService
 * we can restart process when we are at background or screen off
 * Created by zhangshaowen on 16/4/13.
 */
public class SampleResultService extends DefaultTinkerResultService {
    private static final String TAG = "Tinker.SampleResultService";
    @Override
    public void onPatchResult(final PatchResult result) {
        if (result == null) {
            Log.e(TAG, "TinkerResultService received null result!!!!");
            return;
        }
        Log.e(TAG, "TinkerResultService receive result: %s" + result.toString());
        if(result.isSuccess){    //修复成功
            Log.e(TAG,"修复成功");
            deleteRawPatchFile(new File(result.rawPatchFilePath));  //删除补丁包
            Log.e(TAG,"删除补丁"+checkIfNeedKill(result)+"......"+Utils.isBackground());

            if(checkIfNeedKill(result)){
                if(Utils.isBackground()){// 如果app在后台直接重新启动
                    Log.e(TAG,"后台来了");
                    restartProcess();
                }else{// 如果app不再后台就进行注册监听
                    Log.e(TAG,"前台来了");
                    new ScreenState(getApplicationContext(), new ScreenState.IOnScreenOff() {
                        @Override
                        public void onScreenOff() {
                            restartProcess();
                        }
                    });
                }
            }
        }
    }

    /**
     * you can restart your process through service or broadcast
     */
    private void restartProcess() {
        TinkerLog.i(TAG, "app is background now, i can kill quietly");
        //you can send service or broadcast intent to restart your process
        android.os.Process.killProcess(android.os.Process.myPid());// 杀死当前进程
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);// 重启进程
        am.restartPackage("com.example.zy.tinkertest");
    }
    static class ScreenState {
        interface IOnScreenOff {
            void onScreenOff();
        }

        ScreenState(Context context, final IOnScreenOff onScreenOffInterface) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_OFF);// 监听屏幕锁屏事件
            context.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent in) {
                    String action = in == null ? "" : in.getAction();
                    TinkerLog.i(TAG, "ScreenReceiver action [%s] ", action);
                    if (Intent.ACTION_SCREEN_OFF.equals(action)) {

                        context.unregisterReceiver(this);

                        if (onScreenOffInterface != null) {
                            onScreenOffInterface.onScreenOff();
                        }
                    }
                }
            }, filter);
        }
    }

}
